// pages/mine/address/addinto/addinto.js
//获取应用实例
const app = getApp()
var address = [] //地区数据
// var app = getApp()
// 地址相关数据---------------------
// var area = require('../../../../utils/area.js')
import Tips from '../../../class/methods/Tips.js'

Page({
  data: {
    hpUrl: app.globalData.api,
    animationAddressMenu: {},
    addressMenuIsShow: false,
    value: [0, 0, 0],
    provinces: [],
    citys: [],
    areas: [],
    province: '',
    city: '',
    area: '',
    display: "none",
    currentID: '',
    package: [{
      id: 1,
      goodstitle: '是',

    }, {
      id: 0,
      goodstitle: '否',

    }],
  },




  // 修改默认
  bindmr() {
    let that = this
    let addresee_default = that.data.addresee_default
    if (addresee_default == 1) {
      that.setData({
        addresee_default: 0
      })
    } else {
      that.setData({
        addresee_default: 1
      })
    }
  },
  //-----------------------------
  // 表单判断
  // 手机号码
  blurPhonel: function(e) {
    let that = this
    var phone = e.detail.value;
    console.log('手机号', phone)

    that.setData({
      ajxtrue: true,
      phone: phone
    })
    if (phone.length > 11) {
      Tips.alert('号码有误')
    }
  },

  // 表单提交
  formSubmit: function (e) {
    var that = this;
    var user = wx.getStorageSync('user');
    console.log(user)
    var address = e.detail.value;
    var phone = e.detail.value.phone;
    console.log(address)
    // var ajxtrue = this.data.ajxtrue;
    let addresee_default = that.data.addresee_default

    that.setData({

      phone: phone
    })
    let address_provinces_id = that.data.address_provinces_id; //省id

    let address_city_id = that.data.address_city_id; //市id
    let address_areas_id = that.data.address_areas_id; //区县id
    let uid = that.data.uid
    let id = that.data.id
    console.log(address.username, phone, that.data.address_provinces_id, that.data.address_city_id, that.data.address_areas_id, address.address_title, that.data.addresee_default, that.data.id)

    if (phone) {
      if (address.username == '') {
        wx.showToast({
          title: '请填写收件人',
          icon: 'success',
          duration: 2000
        })
      } else if (!(/^1[34578]\d{9}$/.test(phone))) {
        that.setData({
          ajxtrue: false
        })
        console.log(phone.length)
        if (phone.length > 11) {
          wx.showToast({
            title: '手机号有误',
            icon: 'success',
            duration: 2000
          })
        } else if (phone.length < 11) {
          wx.showToast({
            title: '手机号有误',
            icon: 'success',
            duration: 2000
          })
        }
      } else if (!that.data.address_provinces_id) {
        wx.showToast({
          title: '请选择地区',
          icon: 'success',
          duration: 2000
        })
      } else if (address.address_title == '') {
        wx.showToast({
          title: '请填写地址',
          icon: 'success',
          duration: 2000
        })
      } else {
        console.log(that.data.address_provinces_id)
        wx.request({
          url: app.globalData.api + 'doAddAddress',
          header: {
            "Content-Type": "application/x-www-form-urlencoded"
          },
          method: "POST",
          data: {
            uid: that.data.uid,
            username: address.username,
            phone: address.phone,
            address_provinces_id: that.data.address_provinces_id,
            address_city_id: that.data.address_city_id,
            address_areas_id: that.data.address_areas_id,
            addresee_default: that.data.radioid,
            address_title: address.address_title,
          },
          success: function (res) {
            Tips.loaded();
            var result = res.data;
            console.log(result);
            if (result.code == 0) {
              wx.showToast({
                title: '保存成功',
                icon: 'success',
                duration: 2000
              })
              setTimeout(function () {
                // wx.redirectTo({
                //   url: "/pages/mine/address/address"
                // })
                wx.navigateBack({
                  delta: 1
                })
              }, 1500)
              that.setData({
                allAddress: result
              });
            }


          }
        });
      }
    } else {
      wx.showToast({
        title: '请填写手机号',
        icon: 'success',
        duration: 2000
      })
    }



  },

  // 地址相关函数 尾部-----------------
  onLoad: function(options) {
    var that = this;

    wx.getStorage({
      key: 'userinfo',
      success: function(res) {
        console.log('用户身份', res, res.data.id)
        let uid = res.data.id
        that.setData({
          uid: uid

        })

      },
    })



    // 初始化动画变量
    var animation = wx.createAnimation({
      duration: 500,
      transformOrigin: "50% 50%",
      timingFunction: 'ease',
    })
    that.animation = animation;

    // 默认联动显示北京
    address = wx.getStorage({
      key: 'AllArea',
      success: function(res) {
        if (address == null) {
          that.getAllArea();
        } else {
          that.setAllArea();
        }
      },
      fail: function(res) {
        that.getAllArea();
      },
      complete: function(res) {},
    })
  },


  //获取地址库
  getAllArea: function() {
    var that = this;
    wx.request({
      url: app.globalData.api + 'getProvinces',

      success: function(res) {
        console.log('省', res)
        var result = res.data.list;
        that.setData({
          provinces: result
        })

      },

    });
    wx.request({
      url: app.globalData.api + 'getCities',
      data: {
        provinceid: 110000
      },
      success: function(res) {
        console.log('市', res)
        var result = res.data.list;
        that.setData({
          citys: result
        })

      },

    });
    wx.request({
      url: app.globalData.api + 'getAreas',
      data: {
        cityid: 110200
      },
      success: function(res) {
        console.log('区', res)
        var result = res.data.list;
        that.setData({
          areas: result
        })

      },

    });
  },

  //设置默认地址信息
  setAllArea: function() {
    var id = address.provinces[0].id
    this.setData({
      provinces: address.provinces,
      citys: address.citys[id],
      areas: address.areas[address.citys[id][0].id],
    })
  },

  // 点击所在地区弹出选择框
  selectDistrict: function(e) {
    var that = this
    // 如果已经显示，不在执行显示动画
    if (that.data.addressMenuIsShow) {
      return
    }
    // 执行显示动画
    that.startAddressAnimation(true)
  },

  // 执行动画
  startAddressAnimation: function(isShow) {
    var that = this
    if (isShow) {
      // vh是用来表示尺寸的单位，高度全屏是100vh
      that.animation.translateY(0 + 'vh').step();
      //显示遮罩层
      that.setData({
        display: "block"
      })

    } else {
      that.animation.translateY(40 + 'vh').step();
      //隐藏遮罩层
      that.setData({
        display: "none"
      })
    }
    that.setData({
      animationAddressMenu: that.animation.export(),
      addressMenuIsShow: isShow,
    })
  },
  // 点击地区选择取消按钮
  cityCancel: function(e) {
    this.startAddressAnimation(false)
  },
  // 点击地区选择确定按钮
  citySure: function(e) {
    var that = this
    var city = that.data.city
    var value = that.data.value
    console.log()
    that.startAddressAnimation(false)
    // 将选择的城市信息显示到输入框
    var areaInfo = that.data.provinces[value[0]].province + '-' + that.data.citys[value[1]].city + '-' + that.data.areas[value[2]].area;
    var address_provinces_id = that.data.provinces[value[0]].provinceid
    let address_city_id = that.data.citys[value[1]].cityid
    let address_areas_id = that.data.areas[value[2]].areaid;

    var id = that.data.areas[value[2]].id;
    that.setData({
      areaInfo: areaInfo,
      currentID: id,
      address_provinces_id: address_provinces_id,
      address_city_id: address_city_id,
      address_areas_id: address_areas_id
    })
    console.log('城市id', address_provinces_id, address_city_id, address_areas_id)
  },
  // 点击蒙版时取消组件的显示
  hideCitySelected: function(e) {
    this.startAddressAnimation(true)
  },
  // 处理省市县联动逻辑
  cityChange: function(e) {
    let that = this
    console.log(e)
    var value = e.detail.value
    var provinces = this.data.provinces
    var citys = this.data.citys
    var areas = this.data.areas
    var provinceNum = value[0]
    var cityNum = value[1]
    var countyNum = value[2]
    console.log(this.data.value[1], cityNum)
    // 如果省份选择项和之前不一样，表示滑动了省份，此时市默认是省的第一组数据，
    if (this.data.value[0] != provinceNum) {
      var id = provinces[provinceNum].id
      wx.request({
        url: app.globalData.api + 'getCities',
        data: {
          provinceid: provinces[provinceNum].provinceid
        },
        success: function(res) {
          console.log('市', res)
          let result = res.data.list;
          that.setData({
            citys: result
          })
          let cityid = res.data.list[0].cityid
          wx.request({
            url: app.globalData.api + 'getAreas',
            data: {
              cityid: cityid
            },
            success: function(res) {
              console.log('区', res)
              let result = res.data.list;
              that.setData({
                areas: result,
                value: [provinceNum, 0, 0]
              })

            },

          });

        },

      });

    } else if (this.data.value[1] != cityNum) {
      // 滑动选择了第二项数据，即市，此时区显示省市对应的第一组数据
      console.log('市改变时')

      let cityid = citys[cityNum].cityid
      wx.request({
        url: app.globalData.api + 'getAreas',
        data: {
          cityid: cityid
        },
        success: function (res) {
          console.log('区', res)
          let result = res.data.list;
          that.setData({
            areas: result,
            value: [provinceNum, cityNum, 0],
          })

        },

      });
      // this.setData({
      //   value: [provinceNum, cityNum, 0],
      //   // areas: address.areas[citys[cityNum].id],
      // })
    } else {
      // 滑动选择了区
      this.setData({
        value: [provinceNum, cityNum, countyNum]
      })
    }
  },



  // ------------------- 分割线 --------------------
  onReady: function() {

  },

  //页面滑至底部事件
  onReachBottom: function() {
    // Do something when page reach bottom.
  },

})